<template>
    <div class="profile-page">
        <page-header title="个人中心" :show-back-arrow="false" />

        <div class="page-content">
            <!-- 用户信息卡片 -->
            <div class="user-card">
                <div class="avatar">
                    <van-image round width="70" height="70"
                        :src="userInfo.avatar || 'https://img01.yzcdn.cn/vant/cat.jpeg'" />
                </div>
                <div class="user-info">
                    <div class="username">{{ userInfo.nickname || '未登录' }}</div>
                    <div class="user-phone">{{ userInfo.phone || '点击登录账号' }}</div>
                </div>
                <div class="edit-btn" v-if="isLoggedIn">
                    <van-button round plain size="small" @click="goToEditProfile">编辑资料</van-button>
                </div>
                <div class="edit-btn" v-else>
                    <van-button round type="primary" size="small" @click="goToLogin">立即登录</van-button>
                </div>
            </div>

            <!-- 我的服务 -->
            <div class="section">
                <div class="section-title">我的服务</div>
                <div class="grid-services">
                    <van-grid :column-num="4" :border="false" :gutter="10">
                        <van-grid-item icon="records" text="我的预约" to="/appointments" />
                        <van-grid-item icon="friends-o" text="我的就诊人" @click="goToPatients" />
                        <van-grid-item icon="star-o" text="我的收藏" @click="goToFavorites" />
                        <van-grid-item icon="comment-o" text="我的评价" @click="goToReviews" />
                    </van-grid>
                </div>
            </div>

            <!-- 健康服务 -->
            <div class="section">
                <div class="section-title">健康服务</div>
                <div class="service-cells">
                    <van-cell-group inset>
                        <van-cell title="健康档案" icon="description" is-link @click="goToHealthRecords" />
                        <van-cell title="检查报告" icon="notes-o" is-link @click="goToReports" />
                        <van-cell title="体检预约" icon="calendar-o" is-link @click="goToPhysicalExam" />
                        <van-cell title="疫苗接种" icon="bulb-o" is-link @click="goToVaccination" />
                    </van-cell-group>
                </div>
            </div>

            <!-- 系统设置 -->
            <div class="section">
                <div class="section-title">系统设置</div>
                <div class="service-cells">
                    <van-cell-group inset>
                        <van-cell title="账号设置" icon="setting-o" is-link @click="goToAccountSettings" />
                        <van-cell title="消息通知" icon="bell" is-link @click="goToNotifications" />
                        <van-cell title="关于我们" icon="info-o" is-link @click="goToAbout" />
                        <van-cell v-if="isLoggedIn" title="退出登录" icon="close" @click="handleLogout" />
                    </van-cell-group>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue';
import { useRouter } from 'vue-router';
import { showDialog, showToast } from 'vant';
import PageHeader from '@/components/common/PageHeader.vue';
import { useUserStore } from '@/store/user';

interface UserInfo {
    avatar?: string;
    nickname?: string;
    phone?: string;
}

const router = useRouter();
const userStore = useUserStore();

// 用户信息和登录状态
const userInfo = computed<UserInfo>(() => userStore.userInfo || {});
const isLoggedIn = computed(() => userStore.isLoggedIn);

// 页面导航方法
const goToLogin = () => {
    router.push('/login');
};

const goToEditProfile = () => {
    router.push('/profile/edit');
};

const goToPatients = () => {
    router.push('/profile/patients');
};

const goToFavorites = () => {
    // 暂未实现
    showToast('功能开发中');
};

const goToReviews = () => {
    // 暂未实现
    showToast('功能开发中');
};

const goToHealthRecords = () => {
    // 暂未实现
    showToast('功能开发中');
};

const goToReports = () => {
    // 暂未实现
    showToast('功能开发中');
};

const goToPhysicalExam = () => {
    // 暂未实现
    showToast('功能开发中');
};

const goToVaccination = () => {
    // 暂未实现
    showToast('功能开发中');
};

const goToAccountSettings = () => {
    // 暂未实现
    showToast('功能开发中');
};

const goToNotifications = () => {
    // 暂未实现
    showToast('功能开发中');
};

const goToAbout = () => {
    // 暂未实现
    showToast('功能开发中');
};

// 退出登录
const handleLogout = () => {
    showDialog({
        title: '退出提示',
        message: '确定要退出登录吗？',
        showCancelButton: true
    }).then((action) => {
        if (action === 'confirm') {
            userStore.logout();
            showToast('已退出登录');
            router.push('/login');
        }
    });
};
</script>

<style scoped lang="scss">
.profile-page {
    min-height: 100vh;
    background-color: #f5f5f5;
    padding-top: 46px; // 为顶部导航栏预留空间
    padding-bottom: 50px; // 为底部标签栏预留空间

    .page-content {
        padding: 16px;
    }

    .user-card {
        display: flex;
        align-items: center;
        background-color: #fff;
        border-radius: 12px;
        padding: 20px;
        margin-bottom: 16px;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);

        .avatar {
            margin-right: 16px;
        }

        .user-info {
            flex: 1;

            .username {
                font-size: 18px;
                font-weight: 500;
                color: #333;
                margin-bottom: 4px;
            }

            .user-phone {
                font-size: 14px;
                color: #999;
            }
        }

        .edit-btn {
            .van-button {
                border-radius: 50px;
                padding: 0 12px;
            }
        }
    }

    .section {
        margin-bottom: 16px;

        .section-title {
            font-size: 16px;
            font-weight: 500;
            color: #333;
            margin-bottom: 12px;
            padding-left: 8px;
        }

        .grid-services {
            background-color: #fff;
            border-radius: 12px;
            padding: 8px 0;

            :deep(.van-grid-item__text) {
                font-size: 13px;
                margin-top: 4px;
            }

            :deep(.van-grid-item__icon) {
                font-size: 24px;
            }
        }

        .service-cells {
            :deep(.van-cell) {
                font-size: 14px;
                align-items: center;

                .van-cell__left-icon {
                    margin-right: 8px;
                    font-size: 18px;
                }
            }
        }
    }
}
</style>